package cl.crabcom.shoppinglist;

import static cl.crabcom.accessdata.Constants.NAME;
import static cl.crabcom.accessdata.Constants.TABLE_NAME;
import static cl.crabcom.accessdata.Constants.TIME;
import cl.crabcom.accessdata.MarketData;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class NewStore extends Activity implements OnClickListener{	
	   
	private MarketData stores;

	Button btn_guardar = null;
	Button btn_cancelar = null;
	EditText name_market = null;	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newstore);

		this.btn_guardar = (Button) findViewById(R.id.btn_guardar);
		if (this.btn_guardar != null) {
			this.btn_guardar.setOnClickListener(this);
		}
		this.btn_cancelar = (Button) findViewById(R.id.btn_cancelar);
		if (this.btn_cancelar != null) {
			this.btn_cancelar.setOnClickListener(this);
		}		
		
		// Área de texto
		this.name_market = (EditText) findViewById(R.id.name_market);	
	}

	@Override
	public void onClick(View vista) {

		if (vista.getId() == R.id.btn_guardar) {
			
			stores = new MarketData(this);
			try {
				this.addStore(""+this.name_market.getText());
			
			} finally {
				stores.close();
				finish();
			}
			
		}else if(vista.getId() == R.id.btn_cancelar){
			
			finish();
		}
		
	}
	
	//Agregar una tienda
	private void addStore(String string) {

		SQLiteDatabase db = stores.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(TIME, System.currentTimeMillis());
		values.put(NAME, string);
		db.insertOrThrow(TABLE_NAME, null, values);
	}

}
